package cn.rongdao.jrkabao.ui.message.uicomponent;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.message.MessageBean;

/**
 * Created by jack on 2017/8/17
 */

public class MessageUIComponent extends RelativeLayout {
    public MessageUIComponent(Context context) {
        super(context);
    }

    public MessageUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MessageUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ImageView ivAvatar;
    private ImageView ivDotMessage;
    private TextView tvTitle;
    private TextView tvTime;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivAvatar = (ImageView) findViewById(R.id.im_iv_avatar);
        ivDotMessage = (ImageView) findViewById(R.id.im_iv_dot_message);
        tvTitle = (TextView) findViewById(R.id.im_tv_title);
        tvTime = (TextView) findViewById(R.id.im_tv_time);
    }

    public void setContent(MessageBean data) {
        setVisibility(View.VISIBLE);
        FRImageLoad.loadImage(FRString.valueOf(data.getImg_url()),ivAvatar);
        ivDotMessage.setVisibility(data.getIs_read() == 1 ? VISIBLE : GONE);
        tvTitle.setText(getTitleContent(FRString.valueOf(data.getName()), FRString.valueOf(data.getHead())));
        tvTime.setText(FRString.valueOf(data.getTime()));
    }

    private SpannableString getTitleContent(String title, String content) {
        SpannableString ss = new SpannableString(FRCollection.appendAsString(title, content, "\n"));
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.c333333)), 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(16, true), 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }
}